var home=require("../../api/home");
var util=require("../../utils/util");
Page({
  /*页面的初始数据*/
  data: {
    benificList:[
      {
        url:'../../images/vip/1.png',
        title:'优惠券'
      },
      {
        url:'../../images/vip/2.png',
        title:'积分兑换'
      },
      {
        url:'../../images/vip/3.png',
        title:'引荐机构'
      },
      {
        url:'../../images/vip/4.png',
        title:'赠送课时'
      },
      {
        url:'../../images/vip/5.png',
        title:'课程折扣'
      },
      {
        url:'../../images/vip/6.png',
        title:'课程免单'
      },
      {
        url:'../../images/vip/7.png',
        title:'福利优享'
      },
      {
        url:'../../images/vip/8.png',
        title:'生日祝福'
      },
    ],
    vipBenificList:[
      {
        url:'../../images/vip/v1.png',
        title:'优惠券'
      },
      {
        url:'../../images/vip/v2.png',
        title:'积分兑换'
      },
      {
        url:'../../images/vip/v3.png',
        title:'引荐机构'
      },
      {
        url:'../../images/vip/v4.png',
        title:'赠送课时'
      },
      {
        url:'../../images/vip/v5.png',
        title:'课程折扣'
      },
      {
        url:'../../images/vip/v6.png',
        title:'课程免单'
      },
      {
        url:'../../images/vip/v7.png',
        title:'福利优享'
      },
      {
        url:'../../images/vip/v8.png',
        title:'生日祝福'
      },
    ],
    jxCourseList:[],
    isVipStatus:false,
    title:'家长会员',
    navColor:'rgba(0,0,0,0)',
    showMore:true,
    latitude:'',
    longitude:'',
    class_list:[],
    sub_id:'',
    currentPage:1,
    pageSize:4
  },
  /*生命周期函数--监听页面加载*/
  onLoad: function (options) {
    let that = this
    that.get_Home_classify_list()
    wx.getStorage({
      key: 'city',
      success:function(res){
        let info=res.data;
        that.setData({latitude:info.lat,longitude:info.lng})
      }
    })
  },
    // 进入加载数据
    get_Home_classify_list:function(){
      let that = this, arr1 = [], arr2 = []
      home.get_home_classify_list().then(res=>{
        if(res.data.code == '200'){
          const { data } = res.data
          let list = data
          list.forEach(item=>{
            if(item.position == 1){
              arr1.push(item)
            }else{
              arr2.push(item)
            }
            if(item.name === '学科辅导'){
              that.setData({sub_id:item.id})
            }
          })
          that.setData({class_list:arr2})
          that.get_all_child_id(arr2[0].id)
        }
      })
    },
      // 获取小分类所有id
  get_all_child_id:function(id){
    const params ={
      parentId:id,
      flagEnable:1
    }
    home.get_cate_list(params).then(res=>{
      if(res.data.code == 200){
        let childListIds = []
        const { data } = res.data
        data.forEach(item=>{
          childListIds.push(item.id)
        })
        this.get_cour_list(childListIds.join(","))
      }
    })
  },
    // 获取课程列表
    get_cour_list:function(ids){
      console.log(ids);
      let that = this,
          currentPage = that.data.currentPage,
          pageSize = that.data.pageSize
      wx.showLoading({ title: '加载中' })
      const params = {
        "currentPage": currentPage,
        "pageSize": pageSize,
        "status": 3,
        "categoryIds": ids,
        "lat": that.data.latitude,
        "lng": that.data.longitude
      }
      home.get_class_cour_list(params).then(res=>{
        if(res.data.code == 200){
          const { records } = res.data.data
          records.forEach(item=>{
            if(item.introducePictures){
              item.picture = item.introducePictures.split(',')
            }
            if(item.labelName){
              item.labelArray = item.labelName.split(',')
            }
            if(item.distance){
              item.space=item.distance>1000?(item.distance/1000).toFixed(2):item.distance;
            }
          })
          console.log(records);
          if(records.length == 0){
            util.page_show_toast('没有更多了',1500)
          }else{
            this.setData({jxCourseList:this.data.jxCourseList.concat(records)})
          }
        }
      })
    },
  // 顶部导航栏颜色
  onPageScroll:function(e){
    const {scrollTop} = e
    if(scrollTop>30){
      this.setData({navColor:'rgba(64, 68, 87,1)'})
    }else{
      this.setData({navColor: 'rgba(0,0,0,0)'})
    }
  },
  // vip切换
  changeCurrent:function(e){
    const{ current } = e.detail
    switch(current){
      case 0:
        this.setData({isVipStatus:!this.data.isVipStatus})
        break;
      case 1:
        this.setData({isVipStatus:!this.data.isVipStatus})
        break;
      default:
        this.setData({isVipStatus:this.data.isVipStatus})
    }
  },
  navto_rule:function(){
    wx.navigateTo({
      url: '/mypage/memberRule/memberRule',
    })
  },
  navto_twowm:function(){
    wx.navigateTo({
      url: '/mypage/reference/reference',
    })
  },
  nav_pay:function(){
    wx.navigateTo({
      url: '../intoMember/intoMember',
    })
  },
})